package com.cncn.youmengsharelib.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.cncn.youmengsharelib.R;
import com.cncn.youmengsharelib.ShareData;
import com.cncn.youmengsharelib.ShareHelper;
import com.cncn.youmengsharelib.ShareListener;
import com.cncn.youmengsharelib.SharePlatform;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 分享界面
 *
 * @Author: PengWeiwei
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-04-07
 * @Version: 1.0.0
 */
public class SharePlatformsFragment extends DialogFragment {

    public static final String EXTRA_TITLE = "extra_title";
    public static final String EXTRA_CONTENT = "extra_content";
    public static final String EXTRA_IMAGE_URL = "extra_image_url";
    public static final String EXTRA_TARGET_URL = "extra_target_url";

    private List<SharePlatform> platformsList = new ArrayList<>();
    private ShareData mShareInfo;
    //一个对Context的软引用
    private SoftReference<Context> mAppRef;

    private GridView mGvTypes;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        platformsList = getDefaultPlatformInfoes();
        String title = getArguments().getString(EXTRA_TITLE);
        String content = getArguments().getString(EXTRA_CONTENT);
        String imageUrl = getArguments().getString(EXTRA_IMAGE_URL);
        String targetUrl = getArguments().getString(EXTRA_TARGET_URL);

//        imageUrl = "http://pic24.nipic.com/20121025/11197132_095434613172_2.jpg";
//        targetUrl = "http://pic24.nipic.com/20121025/11197132_095434613172_2.jpg";
        if (TextUtils.isEmpty(title)) {
            title = getString(R.string.app_name);
        }

        if (TextUtils.isEmpty(content)) {
            content = getString(R.string.app_name);
        }

        if (TextUtils.isEmpty(imageUrl)) {
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
            mShareInfo = new ShareData(title, content, bmp, targetUrl);
        } else {
            mShareInfo = new ShareData(title, content, imageUrl, targetUrl);
        }
    }

    public static SharePlatformsFragment newInstance(String title, String content, String imageUrl, String targetUrl) {
        Bundle args = new Bundle();
        args.putString(EXTRA_TITLE, title);
        args.putString(EXTRA_CONTENT, content);
        args.putString(EXTRA_IMAGE_URL, imageUrl);
        args.putString(EXTRA_TARGET_URL, targetUrl);
        SharePlatformsFragment fragment = new SharePlatformsFragment();
        fragment.setArguments(args);
        return fragment;
    }


    public SharePlatformsFragment() {
        super();
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_share_platforms, container, false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mGvTypes = (GridView) rootView.findViewById(R.id.gvPlatforms);
        ((Button) rootView.findViewById(R.id.btnCancle)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharePlatformsFragment.this.dismissAllowingStateLoss();
            }
        });
        mGvTypes.setAdapter(new PlateformsAdapter(getActivity(), platformsList));
        mGvTypes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SharePlatform plat = platformsList.get(position);
                ShareHelper.share(getActivity(), plat, mShareInfo, mShareListener);
                SharePlatformsFragment.this.dismissAllowingStateLoss();
            }
        });
        mGvTypes.setSelector(android.R.color.white);
        getDialog().setCanceledOnTouchOutside(true);

        mAppRef = new SoftReference<>(getActivity().getApplicationContext());
        return rootView;
    }


    /**
     * 外部设置监听器
     *
     * @param shareListener
     */
    public void setShareListener(ShareListener shareListener) {
        mShareListener = shareListener;
    }

    private ShareListener mShareListener = new ShareListener() {

        @Override
        public void onShareSuccess(Activity shareActivity, SharePlatform platform) {
            Context context = mAppRef.get();
            Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onShareFailed(Activity shareActivity, SharePlatform platform) {
            Context context = mAppRef.get();
            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onShareCancel(Activity shareActivity, SharePlatform platform) {
            Context context = mAppRef.get();
            Toast.makeText(context, "取消分享", Toast.LENGTH_SHORT).show();
        }
    };


    public static List<SharePlatform> getDefaultPlatformInfoes() {
        SharePlatform info1 = SharePlatform.QQ;
        SharePlatform info2 = SharePlatform.QZONE;
        SharePlatform info3 = SharePlatform.WECHAT;
        SharePlatform info4 = SharePlatform.WECHATMOMENTS;
        SharePlatform info5 = SharePlatform.SINAWEIBO;
        SharePlatform info6 = SharePlatform.SMS;
        // 设置显示的图片
        info1.setDrawableId(R.drawable.qq);
        info2.setDrawableId(R.drawable.qqzone);
        info3.setDrawableId(R.drawable.icon_weixin);
        info4.setDrawableId(R.drawable.icon_pengyouquan);


        List<SharePlatform> list = new ArrayList<>();
        list.add(info1);
        list.add(info2);
        list.add(info3);
        list.add(info4);

        return list;

    }
}
